/**
 * 渠道包js管理
 */
$(function(){
    $('.ckeckSubmit').click(function(){
        var _Name,_PackageName,_OnTime,_Type,_WeiXinUrl,_Forever,_data;
        _Type = $('.typeInfo').val()

        if(_Type!=2){
            _Name = $('input[name=Name]').val();
            if(!checkName(_Name)){
                standardTips('填写渠道包名称');
                return false;
            }
        }

        _PackageName = $('input[name=packageName]').val();
        if(!_PackageName){
            standardTips('上传渠道包');
            return false;
        }

        if(_Type!=2){

            _Forever =  $('input[name=Forever]:checked').val();
            _OnTime = $('input[name=ExpirationTime]').val();

            if(_Forever&&_OnTime){
                standardTips('请选择一种有效期');
                return false;
            }

            if(!_Forever){
                if(!_OnTime){
                    standardTips('请选择时间');
                    return false;
                }
            }

            _WeiXinUrl = $('input[name=WeiXinUrl]').val();
            if(!$.trim(_WeiXinUrl)){
                standardTips('填写微信Url');
                return false;
            }

            var rg = /(https?|http):\/\/([^\.\/]+)([\.]?)([^\.\/]+)([\.]?)([^\.\/]+)(\/[\w\-\.\/\?\%\&\=]*)?/i;
            if(_WeiXinUrl && !rg.test(_WeiXinUrl)){
                standardTips('url格式错误');
                return false;
            }

        }

        _data = {Name:_Name,PackageName:_PackageName,OnTime:_OnTime,WeiXinUrl:_WeiXinUrl,Forever:_Forever,Type:_Type};

        $.post(addPackageUrl, _data, function (data) {
            console.log(data);
            if(data.errno==0){
                standardTips('添加成功',2);
                window.location.href = viewPackageUrl + "&ChannelId=" + data.data;
            }else{
                standardTips('添加失败',2);
            }
        }, 'json')
    })


    //加载更多
    var page = 2;
    $('.JMoreActivity').click(function () {
        var _html='';
        $.ajax({
            url: getMoreActicityUrl,
            type: 'post',
            data: {Page:page},
            timeout: 15000,
            dataType: "json",
            beforeSend: function (XMLHttpRequest) {
                $(".JMoreActivity").html("加载中……");
            },
            success: function (data, textStatus) {
                console.log(data);
                if (data.errno == 0) {
                    if(data.data.package.length>0){
                        $.each(data.data.package, function (key, val) {
                            _html += '<tr>' +
                                '<td><input type="checkbox" data-id=""></td>' +
                                '<td class="act-name">'+val.Name+'<a class="act-view" href="'+getViewUrl+'&ChannelId='+val.ChannelId+'"></a></td>' +
                                '<td>'+val.ChannelId+'</td>' +
                                '<td>'+val.ExpirationTimeInfo+'</td>' +
                                '<td>'+val.WeiXinUrl+'</td>' +
                                '<td>'+val.UpdateTime+'</td>' +
                                '<td>1个链接 <a href="javasctipt:void(0)" class="Jcopy" data-href="">（查看链接）</a>';
                                 _html+=' <input type="hidden" value="'+val.url.result.url_short+'" class="hidUrl">';
                            _html+='</td>';
                            _html +='</tr>';
                        })
                        $('.Joperate tbody').append(_html);
                        $(".JMoreActivity").html("加载更多渠道包链接");
                    }else{
                        $('.JMoreActivity').text('没有更多数据……');
                    }

                }
            },
            complete: function (XMLHttpRequest, textStatus) {
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $(".JMoreActivity").html('加载失败,<a href="#" class="JMoreActivity">点击重试</a>');
            }
        });
        page++;
    });


    ////首页列表查看
    $(".Joperate tbody").on('mouseenter', 'tr', function () {
        var html = '<span class="Jview" title="查看">查看</span>';
        $(this).find(".act-view").html(html);
    });

    $(".Joperate tbody").on('mouseleave', 'tr', function () {
        $(this).find(".act-view").html('');
    });

    //详情页，点击更新加载上传按钮
    $('.updateSite').click(function(){
        var _html='';
        _html += '<br/>' +
            '<div id="packageInfo">'+
            '<form id="packageForm" action="'+uploadifyUrl+'" method="post" enctype="multipart/form-data">' +
            '<input type="file" id="packageupload" name="coverImg" onchange="upfilePackage(this)"></form>'+
            '<span class="form-required uploadStatus"></span>' +
            '<input type="hidden" name="packageName" id="image" class="image">' +
            '</div>' ;

        $('.packageList').html(_html);
        $('.packageOne').hide();
    })


    //编辑信息
    $('.JsaveActivity').click(function(){
        var _Name,_PackageName,_OnTime,_ChannelId,_data,_Type,_WeiXinUrl,_Forever;
        _Name = $('input[name=Name]').val();
        _PackageName = $('.packageSite').html();
        _OnTime = $('input[name=ActiveTime]').val();
        _ChannelId = $('.act_all').data('channelid');
        _Type = $('.act_all').data('type');

        if(_Type==1){
            if(!checkName(_Name)){
                standardTips('填写渠道包名称');
                return false;
            }
        }

        if(!_PackageName){
            standardTips('上传渠道包');
            return false;
        }

        if(_Type==1){
            _Forever =  $('input[name=Forever]:checked').val();

            if(_Forever&&_OnTime){
                standardTips('请选择一种有效期');
                return false;
            }

            if(!_Forever){
                if(!_OnTime){
                    standardTips('请选择有效期时间');
                    return false;
                }
            }
            _WeiXinUrl = $('input[name=WeiXinUrl]').val();
            if(!$.trim(_WeiXinUrl)){
                standardTips('填写微信Url');
                return false;
            }

            var rg = /(https?|http):\/\/([^\.\/]+)([\.]?)([^\.\/]+)([\.]?)([^\.\/]+)(\/[\w\-\.\/\?\%\&\=]*)?/i;
            if(_WeiXinUrl && !rg.test(_WeiXinUrl)){
                standardTips('url格式错误');
                return false;
            }


        }

        _data = {Name:_Name,PackageName:_PackageName,OnTime:_OnTime,ChannelId:_ChannelId,WeiXinUrl:_WeiXinUrl,Forever:_Forever,Type:_Type};

        $.post(editPackageUrl, _data, function (data) {
            if(data.errno==0){
                standardTips('编辑成功',2);
                location.reload();
            }else{
                standardTips('修改失败',2);
            }
        }, 'json')
    })

    //复制链接
    $('.Joperate').on('click','.Jcopy',function(){
        var _url;
        _url = $(this).parents('tr').find('.hidUrl').val();
        console.log(_url);
        standardAllow(_url,function(){

        });
    })

})

    /**
     *
     * @param obj
     */

    function upfilePackage(obj){

        var file = $(obj).val();
        if(!file){
            return false;
        }

        $("#packageForm").ajaxSubmit({
            dataType: 'json',
            beforeSend: function (XMLHttpRequest) {
                $(".uploadStatus").html("上传中……");
            },
            success: function (data) {
                console.log(data);
                $(".uploadStatus").html('');
                if (data.res == 0) {
                    $(obj).parents('.editBusPic').find('.packageSite').html(data.info);
                    $(obj).parents("#packageInfo").find(".image").val(data.info);
                    standardTips('上传成功！');
                } else {
                    alert(data.error);
                }
            }
        });
    }


    /**
     * 检测名称是否完全
     * @param name
     * @returns {boolean}
     */
    function checkName(name){
        var _parmName,checkName;
        _parmName = $.trim(name);
        checkName = /^.{1,}$/;
        if(!checkName.test(_parmName)){
            return false;
        }
        return true;
    }



